package com.yanchangapp.ui.activity.purchase.approve;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luck.picture.lib.decoration.RecycleViewDivider;
import com.luck.picture.lib.tools.ScreenUtils;
import com.yanchangapp.R;
import com.yanchangapp.adapter.PassAvCommentAdapter;
import com.yanchangapp.adapter.PassAvProductAdapter;
import com.yanchangapp.commom.LoadingType;
import com.yanchangapp.commom.RecordStatus;
import com.yanchangapp.model.bean.PassApproveBean;
import com.yanchangapp.model.bean.PassApproveCommentBean;
import com.yanchangapp.model.bean.PassApproveProductBean;
import com.yanchangapp.ui.presenter.ApprovePassDetailAtPresenter;
import com.yanchangapp.ui.view.ApprovePassDetailAtView;
import com.yanchangapp.widget.SpannableMoney;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.util.formatter.BigDecimalUtils;
import com.zxl.baselib.util.ui.ResHelper;
import com.zxl.baselib.util.ui.UIUtils;
import com.zxl.baselib.widget.NiceNestedScrollView;
import com.zxl.baselib.widget.YCLoadingView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 已审批详情界面...
 *
 * 标志是已通过... ok的就是这样的
 */
public class PassApprovePassDetailActivity extends BaseActivity<ApprovePassDetailAtView, ApprovePassDetailAtPresenter> implements ApprovePassDetailAtView {
    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View vToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.tvSupplierTitle)
    TextView tvSupplierTitle;
    @BindView(R.id.tvSupplier)
    TextView tvSupplier;
    @BindView(R.id.tvBuyerNameTitle)
    TextView tvBuyerNameTitle;
    @BindView(R.id.tvBuyerName)
    TextView tvBuyerName;
    @BindView(R.id.tvTimeTitle)
    TextView tvTimeTitle;
    @BindView(R.id.tvTime)
    TextView tvTime;
    @BindView(R.id.rvOrderDetail)
    RecyclerView rvOrderDetail;
    @BindView(R.id.llApproveTitle)
    LinearLayout llApproveTitle;
    @BindView(R.id.rvApprove)
    RecyclerView rvApprove;
    @BindView(R.id.ivImage)
    ImageView ivImage;
    @BindView(R.id.rlApproveEmpty)
    RelativeLayout rlApproveEmpty;
    @BindView(R.id.scrollView)
    NiceNestedScrollView scrollView;
    @BindView(R.id.btnNextRefresh)
    TextView btnNextRefresh;
    @BindView(R.id.llErrorNetwork)
    LinearLayout llErrorNetwork;
    @BindView(R.id.ycLoading)
    YCLoadingView ycLoading;
    @BindView(R.id.tvTag)
    TextView tvTag;
    @BindView(R.id.bottom)
    RelativeLayout bottom;
    @BindView(R.id.tvTotalNumMoney)
    TextView tvTotalNumMoney;

    private String mID;
    private static final String ID = "id";
    private List<PassApproveProductBean> mProductBeans = new ArrayList<>();
    private PassAvProductAdapter mPassAvProductAdapter;
    private List<PassApproveCommentBean> mCommentBeans = new ArrayList<>();
    private PassAvCommentAdapter mPassAvCommentAdapter;

    public static void show(Activity activity, String id) {
        Intent intent = new Intent(activity, PassApprovePassDetailActivity.class);
        intent.putExtra(ID, id);
        activity.startActivity(intent);
    }

    @Override
    protected void init() {
        if (getIntent() != null && getIntent().hasExtra(ID)) {
            mID = getIntent().getStringExtra(ID);
        } else {
            finish();
        }
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_has_approve_detail;
    }

    @Override
    protected ApprovePassDetailAtPresenter createPresenter() {
        return new ApprovePassDetailAtPresenter(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvToolbarTitle.setText("审批详情");
        initAdapter();
    }

    private void initAdapter() {
        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        manager.setSmoothScrollbarEnabled(true);
        manager.setAutoMeasureEnabled(true);
        rvOrderDetail.setHasFixedSize(true);
        rvOrderDetail.setNestedScrollingEnabled(false);
        rvOrderDetail.setLayoutManager(manager);
        mPassAvProductAdapter = new PassAvProductAdapter(R.layout.item_product_record_approve,mProductBeans);
        rvOrderDetail.setAdapter(mPassAvProductAdapter);
        // 设置评论的Adapter
        LinearLayoutManager managerComment = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        managerComment.setSmoothScrollbarEnabled(true);
        managerComment.setAutoMeasureEnabled(true);
        rvApprove.setHasFixedSize(true);
        rvApprove.setNestedScrollingEnabled(false);
        rvApprove.setLayoutManager(managerComment);
        rvApprove.addItemDecoration(new RecycleViewDivider(
                this, LinearLayoutManager.HORIZONTAL, ScreenUtils.dip2px(this,0.5f), ContextCompat.getColor(this, R.color.borderColorBase)));
        mPassAvCommentAdapter = new PassAvCommentAdapter(R.layout.item_comment_record_approve,mCommentBeans);
        rvApprove.setAdapter(mPassAvCommentAdapter);
    }

    @Override
    protected void initData() {
        setLoadingState(LoadingType.LOADING);
        mPresenter.queryAvPassDetail(mID);
    }

    @Override
    protected void initListener() {
        ivToolbarNavigation.setOnClickListener(v -> finish());
        btnNextRefresh.setOnClickListener(v -> initData());
    }

    private void setLoadingState(LoadingType loadingType){
        switch (loadingType){
            case LOADING:
                if (ycLoading.getVisibility() == View.GONE){
                    ycLoading.setVisibility(View.VISIBLE);
                }

                if (scrollView.getVisibility() == View.VISIBLE){
                    scrollView.setVisibility(View.GONE);
                }

                if (llErrorNetwork.getVisibility() == View.VISIBLE){
                    llErrorNetwork.setVisibility(View.GONE);
                }
                break;
            case SUCCESS:

                if (ycLoading.getVisibility() == View.VISIBLE){
                    ycLoading.setVisibility(View.GONE);
                }

                if (scrollView.getVisibility() == View.GONE){
                    scrollView.setVisibility(View.VISIBLE);
                }


                if (llErrorNetwork.getVisibility() == View.VISIBLE){
                    llErrorNetwork.setVisibility(View.GONE);
                }
                break;
            case ERROR:
                if (ycLoading.getVisibility() == View.VISIBLE){
                    ycLoading.setVisibility(View.GONE);
                }

                if (scrollView.getVisibility() == View.VISIBLE){
                    scrollView.setVisibility(View.GONE);
                }


                if (llErrorNetwork.getVisibility() == View.GONE){
                    llErrorNetwork.setVisibility(View.VISIBLE);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void queryPassAvDetailSuccess(PassApproveBean passApproveBean) {
        tvTag.setText("已通过");
        tvTag.setBackground(ResHelper.getDrawable(R.drawable.tag_order_pass));
        tvSupplier.setText(passApproveBean.getSupplierName());
        setLoadingState(LoadingType.SUCCESS);
        tvBuyerName.setText(getString(R.string.order_detail_name_dept,passApproveBean.getBuyerName(),passApproveBean.getDepartmentName()));
        // 设置创建时间...
        tvTime.setText(passApproveBean.getOrderDate());
        // 刷新商品列表的数据
        mProductBeans.addAll(passApproveBean.getProducts());
        mPassAvProductAdapter.notifyDataSetChanged();
        // 刷新评论列表数据
        mCommentBeans.addAll(passApproveBean.getAuditLogs());
        mPassAvCommentAdapter.notifyDataSetChanged();
        if (mCommentBeans.size() == 0){
            rlApproveEmpty.setVisibility(View.VISIBLE);
        }else {
            rlApproveEmpty.setVisibility(View.GONE);
        }
        setTotalMoney(passApproveBean.getTotalMoney(),mProductBeans.size());
    }

    private void setTotalMoney(double money,int size) {
        String totalMoney = BigDecimalUtils.keep2Decimal(money);
        int len = totalMoney.length();
        SpannableString spannableString = new SpannableString(
                ResHelper.getString(R.string.down_order_sum, String.valueOf(size), totalMoney));
        int totalLength = spannableString.length();
        spannableString.setSpan(new SpannableMoney(PassApprovePassDetailActivity.this),
                totalLength - len - 1, totalLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvTotalNumMoney.setText(spannableString);
    }


    @Override
    public void queryPassAvDetailFailure(String err) {
        setLoadingState(LoadingType.ERROR);
        UIUtils.showToast(err);
    }
}
